package org.zhigong.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.zhigong.dto.SysResult;
import org.zhigong.dto.SysSearch;
import org.zhigong.entity.Menu;
import org.zhigong.service.MenuService;

import java.util.List;

import static org.zhigong.util.CommonUtil.isEmpty;
import static org.zhigong.util.CommonUtil.isNumeric;

/**
 * Created by Administrator on 2018/5/10 0010.
 */
@Controller
public class MenuController {

    @Autowired
    private MenuService menuService;

    // 只做跳转用,初始化使用json获取数据处理.
    @RequestMapping(value = "/menus", method = RequestMethod.GET)
    public String init(){
        return "menu";
    }

    // 分页查询获取列表,返回json
    @RequestMapping(
            value = "/menus",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<List<Menu>> search(SysSearch sysSearch){

        SysResult<List<Menu>> sysResult;
        if(sysSearch.getPageNumber() < 0){
            try{
                List<Menu> bumenList = menuService.getMenuListAll();
                sysResult = new SysResult<List<Menu>>(true, bumenList);
            }catch (Exception e){
                sysResult = new SysResult<List<Menu>>(false, e.getMessage());
            }
        }else{
            Menu menu = new Menu();
            String condS = sysSearch.getCondition();
            menu.setName(condS);
            menu.setUrl(condS);
            menu.getPage().setCurrentPage(sysSearch.getCurrentPage());
            menu.getPage().setPageNumber(sysSearch.getPageNumber());

            try{
                List<Menu> MenuList = menuService.getMenuList(menu);
                if(MenuList.size() > 0){
                    MenuList.get(0).setPage(menu.getPage());
                }
                sysResult = new SysResult<List<Menu>>(true, MenuList);
            }catch (Exception e){
                sysResult = new SysResult<List<Menu>>(false, e.getMessage());
            }
        }

        return sysResult;
    }
    // 新增
    @RequestMapping(
            value = "/menus",
            method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Integer> create(@RequestBody Menu menu){
        SysResult<Integer> sysResult;
        try{
            Boolean add  = menuService.addMenu(menu);
            sysResult = new SysResult<Integer>(true, add ? 1 : 0);
        }catch (Exception e){
            sysResult = new SysResult<Integer>(false, e.getMessage());
        }
        return sysResult;
    }


    // 删除
    @RequestMapping(
            value = "/menus/{id}",
            method = RequestMethod.DELETE,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Integer> delete(@PathVariable("id") Long id){
        SysResult<Integer> sysResult;
        try{
            Boolean del = menuService.deleteMenu(id);
            sysResult = new SysResult<Integer>(true, del ? 1 : 0);
        }catch(Exception e){
            sysResult = new SysResult<Integer>(false, e.getMessage());
        }
        return sysResult;
    }

    // 更新
    @RequestMapping(
            value = "/menus/{id}",
            method = RequestMethod.PUT,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Integer> update(@RequestBody Menu menu){
        SysResult<Integer> sysResult;
        try{
            Boolean upd = menuService.updateMenu(menu);
            sysResult = new SysResult<Integer>(true, upd ? 1 : 0);
        }catch(Exception e){
            sysResult = new SysResult<Integer>(false, e.getMessage());
        }
        return sysResult;
    }

    // 查看
    @RequestMapping(
            value = "/menus/{id}",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Menu> view(@PathVariable("id") Long id){
        SysResult<Menu> sysResult;
        try{
            Menu menu = menuService.getMenuById(id);
            sysResult = new SysResult<Menu>(true, menu);
        }catch(Exception e){
            sysResult = new SysResult<Menu>(false, e.getMessage());
        }
        return sysResult;
    }

}
